﻿package {
	import flash.display.*;
	import flash.text.*;
	import flash.events.Event;
	[SWF(width="636", height="464")]

	public class ReflectedPacket extends Sprite {
		public var center: Number;
		public var spread: Number;
		public var color: uint;
		private var wave_x: Number;
		private var delta_x: Number;
		private var reflector: Number;
		private var speed: Number = 4;
		private var i: uint = 0;
		public var controlpanel:ControlPanel;
		public var ring:Shape;

		public function ReflectedPacket(center:Number = 100, spread:Number = 20, color:uint=0xff0000) {
			this.center = center;
			this.spread = spread;
			this.color = color;
			init();
		}
		public function init():void {

			spread = 50;
			center = 100;
			wave_x = 10;
			delta_x = 2;
			reflector = 400;
			graphics.lineStyle(3, 0x000000, 1);
			graphics.moveTo(reflector, 100);
			graphics.lineTo(reflector, 300);
			trace(stage.stageHeight);
			controlpanel = new ControlPanel(stage.stageHeight, stage.stageWidth);
			addChild(controlpanel);
			ring = new Shape()
			addChild(ring);
			ring.graphics.lineStyle(1, 0x888888, 1);
			ring.graphics.beginFill(0x000000);
			ring.graphics.drawRoundRect(0,0, 6,4, 5, 2);
			ring.graphics.endFill();

			addEventListener(Event.ENTER_FRAME, onEnterFrame);

		}
		public function onEnterFrame(event:Event):void {
			drawframe();
		}
		public function drawframe():void{
			if (center < 700) {
				trace(stage.stageHeight,stage.stageWidth);
				graphics.clear();
				if(!controlpanel.go) {center += speed;}
				if(controlpanel.reset) {
					center = 100; 
					wave_x = 10;
				}


				graphics.lineStyle(3, 0x000000, 1);
				graphics.moveTo(reflector, 100);
				graphics.lineTo(reflector, 300);

				graphics.lineStyle(1, 0x000000, 1);
				graphics.moveTo(reflector, 200);
				graphics.lineTo(0, 200);
				//graphics.moveTo(10, 200);

				graphics.lineStyle(1, 0xff0000, 1);

				for (i = 0; i < (reflector/delta_x)-4; i++) {
					wave_x = 10 + i*delta_x;

					graphics.lineTo(wave_x, 200 + 50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread))) 
					+ 50*Math.exp(-((wave_x - (2*reflector - center))*(wave_x - (2*reflector - center))/(spread*spread))));
				}
				ring.x = wave_x - 3;
				ring.y =  200 + 50*Math.exp(-((wave_x - center)*(wave_x - center)/(spread*spread)))
				+ 50*Math.exp(-((wave_x - (2*reflector - center))*(wave_x - (2*reflector - center))/(spread*spread)))-2;
			}
			else {
				center = 100;
				wave_x = 10;
				controlpanel.go = true;
			}
			
		}
	}
}